package com.chinastock.communication;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.ArrayList;
import java.util.List;

/**
 * 微服务响应实体,ESB临时使用
 * @author sundegang	
 * 2018年3月28日15:20:48
 */
public class BaseResponse<Data> {
	/** 响应码 */
	@JSONField(name = "meta",serialzeFeatures= {SerializerFeature.WriteMapNullValue})
	private Meta meta;

	/** 响应数据(银河接口规范要求此对象为数组。使用者可以忽略) */
	@JSONField(name = "data",serialzeFeatures= {SerializerFeature.WriteMapNullValue})
	private Data data;
    /**
     * 	分页信息
     */
	@JSONField(name = "pageInfo")
	private BasePageInfo pageInfo;
	
	private boolean convert;

	public BaseResponse() {
		this.meta = new Meta();
		this.meta.setSuccess(true);
		this.meta.setCode("0000");
		this.meta.setMessage("");

	}
	public BaseResponse(Boolean success, String code, String message) {
		if (this.meta == null) {
			this.meta = new Meta();
		}
		this.meta.setSuccess(success);
		this.meta.setCode(code);
		this.meta.setMessage(message);
	}
	
	public void setMeta(Boolean success, String code, String message) {
		if (this.meta == null) {
			this.meta = new Meta();
		}
		this.meta.setSuccess(success);
		this.meta.setCode(code);
		this.meta.setMessage(message);
	}

	public Meta getMeta() {
		return this.meta;
	}

	public void setMeta(Meta meta) {
		this.meta = meta;
	}

	public Data getData() {
		if(this.convert) {
			return (Data)((List)this.data).get(0);
		} else {
			return this.data;
		}
	}

	public void setData(Data data) {
		if(data instanceof List) {
			this.data = data;
		} else {
			this.convert = true;
			List list = new ArrayList();
			list.add(data);
			this.setData((Data)list);
		}
	}

	public BasePageInfo getPageInfo() {
		return this.pageInfo;
	}

	public void setPageInfo(BasePageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}
	@Override
	public String toString() {
		return "BaseResponse [meta=" + meta + ", data=" + data + ", pageInfo=" + pageInfo + ", convert=" + convert
				+ "]";
	}

	
}
